package com.project.controller;

import com.project.bean.CartBean;
import com.project.bean.UserBean;
import com.project.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    @RequestMapping("buy")
    public String buy(CartBean cart, HttpServletRequest req) {
        //得到登陆用户信息
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        cart.setUserId(user.getId());

        cartService.add(cart);
        return "ok";
    }

    @RequestMapping("remove")
    public String remove(Integer id) {
        cartService.del(id);
        return "ok";
    }

    @RequestMapping("update")
    public String update(Integer id, Integer num) {
        cartService.update(id, num);
        return "ok";
    }

    @RequestMapping("findCart")
    public List<CartBean> findCart(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        return cartService.findByUser(user.getId());
    }
}
